# -*- coding: utf-8 -*-

import sqlite3

from . import config

class Message:

    def __init__(self, mid=None):
        self.mid = mid
        self.from_id = None
        self.from_name = None
        self.to_id = None
        self.to_name = None
        self.content = None
        self.time = None
        if mid:
            with sqlite3.connect(config.DATEBASE) as db:
                cur = db.cursor()
                data = cur.execute(
                    """SELECT * FROM msgs WHERE mid = ?""",
                        (mid, )).fetchone()
                if not data:
                    raise ValueError("wrong mid")
                self.from_id = data[1]
                self.from_name = data[2]
                self.to_id = data[3]
                self.to_name = data[4]
                self.content = data[5]
                self.time = data[6]
